class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int,int> mp;
        int ret = 0;
        int pre = 0;
        for(int i = 0;i < nums.size();i++){
            if(nums[i] == 0){
                nums[i] = -1;
            }
            pre += nums[i];
            
            if(pre == 0){
                ret = max(ret,i+1);
            }
            if(mp.count(pre)){
                ret = max(ret,i - mp[pre]);
            }else{
                mp[pre] = i;
            }
        }
        return ret;
    }
};
